#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/11/20 8:15
# @Author  : huidong.bai
# @File    : my_test.py
# @Software: PyCharm
import ctypes
import sys
from ctypes import wintypes
from PyQt5.QtWidgets import QWidget, QApplication, QLabel

RegisterDeviceNotification = ctypes.windll.user32.RegisterDeviceNotificationW
UnregisterDeviceNotification = ctypes.windll.user32.UnregisterDeviceNotification
# class GUID(ctypes.Structure):
#     _fields_ = [
#         ('Data1', wintypes.DWORD),
#         ('Data2', wintypes.WORD),
#         ('Data3', wintypes.WORD),
#         ('Data4', wintypes.BYTE * 8),
#     ]
#
#     def __str__(self):
#         return "{{{:08x}-{:04x}-{:04x}-{}-{}}}".format(
#             self.Data1,
#             self.Data2,
#             self.Data3,
#             ''.join(["{:02x}".format(d) for d in self.Data4[:2]]),
#             ''.join(["{:02x}".format(d) for d in self.Data4[2:]]),
#         )


# USB设备发生变化表示
WM_DEVICECHANGE = 0x0219
# 设备插入完成标识
DBT_DEVICEARRIVAL = 0x8000
# 设备移除完成表示
DBT_DEVICEREMOVECOMPLETE = 0x8004

DBT_DEVTYP_DEVICEINTERFACE = 5
DEVICE_NOTIFY_WINDOW_HANDLE = 0x00000000


class GUID(ctypes.Structure):
    _pack_ = 1
    _fields_ = [("Data1", ctypes.c_ulong),
                ("Data2", ctypes.c_ushort),
                ("Data3", ctypes.c_ushort),
                ("Data4", ctypes.c_ubyte * 8)]


GUID_DEVINTERFACE_LIST = [
    # USB设备
    GUID(0xA5DCBF10, 0x6530, 0x11D2, (ctypes.c_ubyte * 8)(0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED)),
    # DISK设备
    GUID(0x53f56307, 0xb6bf, 0x11d0, (ctypes.c_ubyte * 8)(0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b)),
    # HID设备,
    GUID(0x4D1E55B2, 0xF16F, 0x11CF, (ctypes.c_ubyte * 8)(0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30)),
    # GUID_NDIS_LAN_CLASS
    GUID(0xad498944, 0x762f, 0x11d0, (ctypes.c_ubyte * 8)(0x8d, 0xcb, 0x00, 0xc0, 0x4f, 0xc3, 0x35, 0x8c)),
    # 串口设备
    GUID(0x86e0d1e0, 0x8089, 0x11d0, (ctypes.c_ubyte * 8)(0x9c, 0xe4, 0x08, 0x00, 0x3e, 0x30, 0x1f, 0x73)),
    # GUID_DEVINTERFACE_SERENUM_BUS_ENUMERATOR
    GUID(0x4D36E978, 0xE325, 0x11CE, (ctypes.c_ubyte * 8)(0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18)),
    # GUID_DEVINTERFACE_PARALLEL
    GUID(0x97F76EF0, 0xF883, 0x11D0, (ctypes.c_ubyte * 8)(0xAF, 0x1F, 0x00, 0x00, 0xF8, 0x00, 0x84, 0x5C)),
    # GUID_DEVINTERFACE_PARCLASS
    GUID(0x811FC6A5, 0xF728, 0x11D0, (ctypes.c_ubyte * 8)(0xA5, 0x37, 0x00, 0x00, 0xF8, 0x75, 0x3E, 0xD1))
]


class DEV_BROADCAST_DEVICEINTERFACE(ctypes.Structure):
    _pack_ = 1
    _fields_ = [("dbcc_size", wintypes.DWORD),
                ("dbcc_devicetype", wintypes.DWORD),
                ("dbcc_reserved", wintypes.DWORD),
                ("dbcc_classguid", GUID),
                ("dbcc_name", ctypes.c_wchar * 260)]


class DEV_BROADCAST_HDR(ctypes.Structure):
    _fields_ = [("dbch_size", wintypes.DWORD),
                ("dbch_devicetype", wintypes.DWORD),
                ("dbch_reserved", wintypes.DWORD)]


class DEV_BROADCAST_PORT(ctypes.Structure):
    _pack_ = 1
    _fields_ = [("dbcp_size", wintypes.DWORD),
                ("dbcp_devicetype", wintypes.DWORD),
                ("dbcp_reserved", wintypes.DWORD),
                ("dbcp_name", wintypes.WCHAR * 256)]


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(300, 300)
        self.lab_sn = QLabel(self)
        self.lab_sn.setText('测试一下')

    def nativeEvent(self, eventType, message):
        tag, result = super().nativeEvent(eventType, message)
        if eventType == "windows_generic_MSG":
            msg = wintypes.MSG.from_address(message.__int__())

            if msg.message == WM_DEVICECHANGE:
                import time
                import os
                time.sleep(1)
                devices = []
                pops = os.popen('adb devices').readlines()
                for line in pops:
                    if '\tdevice' in line:
                        devices.append(line.replace('\tdevice\n', ''))

                print(devices)
        return tag, result


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec())
